package my.imgassistant;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import my.commons.ReadCfg;
import my.utils.Constant;

import org.apache.log4j.Logger;


/**
 * @class GetImageSerlvet
 * @author 吴建华
 * @date Sep 1, 2011
 * @time 8:56:20 PM
 * @DESC 获取图象信息
 * @version V1.0
 */
public class GetImageProxySerlvet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;

	private static Logger _log = Logger.getLogger(GetImageProxySerlvet.class);
	
	// 图象存储路径
	private static String imgpath_str = "imgpath";
	// 图象名称
	private static String imgname_str = "imgname";
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String applicationPath = ReadCfg.getProp("CIMS.IMG.BASEPATH");
		String imgpath = request.getParameter(imgpath_str);
		String imgname = request.getParameter(imgname_str);
		String imgrealpath = applicationPath + File.separator  + Constant.CIMS_RESOURCE_BASEPATH + File.separator + imgpath + File.separator + imgname;
		response.setContentType("image/jpg");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Content-Disposition", "attachment;filename=" + imgname);
		OutputStream sout = null;
		try{
			BufferedInputStream istream = new BufferedInputStream(new FileInputStream(imgrealpath));
			sout = response.getOutputStream();
			byte[] b = new byte[8192];
			int len = 0;
			while((len = istream.read(b)) != -1){
				sout.write(b, 0, len);
			}
		}catch(Exception ex){
			_log.error("返回图象信息失败,路径为:" + imgrealpath, ex);
		}finally{
			if(null != sout)
				sout.close();
		}
	}
}
